import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class StudentRecordsFrame extends JFrame implements ActionListener
{
    // Attributes
    // Declare outside the constructor so that they can be accessed by 
    // all methods

    JTextField nameTextField, addrTextField, phoneTextField; 

	StudentRecordsSystem recordsSystem;

	


    public StudentRecordsFrame() 
	{
		recordsSystem = new StudentRecordsSystem(this);

		// Set the layout of the this - grid layout, 2 rows, 2 
		// columns,horizontal and vertical spacing 10 pixels
		setLayout(new GridLayout(4,2,10,10));

		this.add(new JLabel("Name"));
		nameTextField = new JTextField(20);
		this.add(nameTextField);

		this.add(new JLabel("Address"));
		addrTextField = new JTextField(20);
		this.add(addrTextField);

		this.add(new JLabel("Phone number"));
		phoneTextField = new JTextField(20);
		this.add(phoneTextField);

		JButton b = new JButton("Add");
		this.add(b);
		b.addActionListener(this);

		JButton b2 = new JButton("Search");
		this.add(b2);
		b2.addActionListener(this);
    }


	public void actionPerformed(ActionEvent ev)
	{
		if(ev.getActionCommand().equals("Add"))
		{
			String name=JOptionPane.showInputDialog("Enter name");
			String address=JOptionPane.showInputDialog("Enter address");
			String phone=JOptionPane.showInputDialog("Enter phone");

			recordsSystem.addStudent(name,address,phone);
		}
		else if (ev.getActionCommand().equals("Search"))
		{
			String name=JOptionPane.showInputDialog("Enter name");
			recordsSystem.search(name);
		}
	}

	public void setDetails(String name,String address,String phone)
	{
		nameTextField.setText(name);
		addrTextField.setText(address);
		phoneTextField.setText(phone);
	}

	public void showMessage(String message)
	{
		JOptionPane.showMessageDialog(this,message);
	}	

    public static void main (String args[])
    {
       StudentRecordsFrame f = new StudentRecordsFrame();
	   f.pack();
	   f.setVisible(true);
    }
}
